package com.example.wan1.view.wechat;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.wan1.R;
import com.example.wan1.base.adapter.SubPagerAdapter;
import com.example.wan1.base.fragment.BaseFragment;
import com.example.wan1.constant.Constants;
import com.example.wan1.model.ArticleBean;
import com.example.wan1.model.KnowledgeBean;
import com.example.wan1.model.common.CommonResList;
import com.example.wan1.utils.SPUtils;
import com.example.wan1.utils.StatusBarUtil;
import com.example.wan1.utils.ToolsUtils;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import dagger.android.support.AndroidSupportInjection;

/***
 * @Author: zxd
 * @CreateDate: 2021/5/13 11:14
 * @Description: 微信公众号碎片
 */
public class WxFragment extends BaseFragment implements WeChatContract.View {

    @BindView(R.id.wechat_tab)
    TabLayout mWechatTab;
    @BindView(R.id.wechat_vp)
    ViewPager mWechatVp;
    @Inject
    List<String> mTitles;
    @Inject
    List<Fragment> mFragments;
    @Inject
    SubPagerAdapter mAdapter;
    @Inject
    List<KnowledgeBean> mList;
    @Inject
    List<Integer> mIds;
    @Inject
    WechatPresenter mPresenter;
    @BindView(R.id.fake_status_bar)
    View mFakeStatusBar;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        AndroidSupportInjection.inject(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initData() {
        mPresenter.getWechatTitleList();
    }

    @Override
    protected void initView() {
        mList = new ArrayList<>();
        mIds = new ArrayList<>();
        mWechatTab.setupWithViewPager(mWechatVp);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWechatTab.setElevation(0);
        }

        int color = ToolsUtils.getThemeColor(getActivity());
        if (color != 0) {
            //StatusBarUtil.setColor(getActivity(), color, 0);
            mWechatTab.setSelectedTabIndicatorColor(color);
            mWechatTab.setTabTextColors(Color.parseColor("#333333"), color);
        }
//        if (ToolsUtils.getNightMode(getActivity()))
//            StatusBarUtil.setTransparent(getActivity());
    }

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_wx;
    }

    @Override
    public void onGetTitle(List<KnowledgeBean> titles) {
        mList.clear();
        mTitles.clear();
        mFragments.clear();
        mIds.clear();

        mList.addAll(titles);
        for (int i = 0; i < titles.size(); i++) {
            mIds.add(titles.get(i).getId());
            mTitles.add(titles.get(i).getName());
            mFragments.add(new WeChatFragment().newInstance(titles.get(i).getId()));
        }
        mAdapter.setData(mFragments, mTitles);
        mWechatVp.setAdapter(mAdapter);

        String titleStr = (String) SPUtils.get(mContext, Constants.SP_WX_TITLES, "");
        if (TextUtils.isEmpty(titleStr)) {
            //list转String数组
            String str = "";
            for (int i = 0; i < mTitles.size(); i++) {
                if (i != mTitles.size() - 1)
                    str += mTitles.get(i) + ",";
                else
                    str += mTitles.get(i);
            }
            //String数组转为带分隔符的字符串
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                SPUtils.put(mContext, Constants.SP_WX_TITLES, str);
            }
        } else {
            List<String> list = Arrays.asList(titleStr);
            if (list.containsAll(mTitles) && mTitles.containsAll(list)) {
                // do nothing
            } else {
                //list转String数组
                String str = "";
                for (int i = 0; i < mTitles.size(); i++) {
                    if (i != mTitles.size() - 1)
                        str += mTitles.get(i) + ",";
                    else
                        str += mTitles.get(i);
                }
                //String数组转为带分隔符的字符串
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    SPUtils.remove(mContext, Constants.SP_WX_TITLES);
                    SPUtils.put(mContext, Constants.SP_WX_TITLES, str);
                }
            }
        }
    }

    @Override
    public void onFailure(String msg) {
        //如果网络没有的话，会请求标题组失败，使用本地缓存
        String titleStr = (String) SPUtils.get(getActivity(), Constants.SP_WX_TITLES, "");
        List<String> list = Arrays.asList(titleStr.split(","));
        if (list.size() != 0) {
            mTitles = list;
            for (int i = 0; i < list.size(); i++) {
                mFragments.add(new WeChatFragment().newInstance(0));
            }
            mAdapter.setData(mFragments, mTitles);
            mWechatVp.setAdapter(mAdapter);
        }
    }

    //这两个这里暂时用不到
    @Override
    public void onSuccess(CommonResList<ArticleBean> data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showError() {

    }

    public void setTvTitleBackgroundColor(int color) {
        if (mFakeStatusBar == null) return;
        mFakeStatusBar.setBackgroundColor(color);
    }
}
